package com.example.ayniu.treadmill.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.ayniu.treadmill.ClassActivity;
import com.example.ayniu.treadmill.R;

import java.util.ArrayList;
import java.util.List;

public class Fragment_class extends Fragment implements View.OnClickListener{

	private RelativeLayout layout_checkeffect;
	private Button bt_maineffect1,bt_maineffect2,bt_maineffect3,bt_maineffect4,bt_maineffect5;
	private Button bt_assisteffect1,bt_assisteffect2,bt_assisteffect3,bt_assisteffect4,bt_assisteffect5,bt_assisteffect6;
	private Button bt_freeclass1,bt_freeclass2;
	private Button bt_openclass;

	//计算Toast的出现时间
	private long firstTimeOfToast;

	private int MAIN_EFFECT_1 = 1;
	private int MAIN_EFFECT_2 = 2;
	private int MAIN_EFFECT_3 = 4;
	private int MAIN_EFFECT_4 = 8;
	private int MAIN_EFFECT_5 = 16;
	private int ASSIST_EFFECT_1 = 32;
	private int ASSIST_EFFECT_2 = 64;
	private int ASSIST_EFFECT_3 = 128;
	private int ASSIST_EFFECT_4 = 256;
	private int ASSIST_EFFECT_5 = 512;
	private int ASSIST_EFFECT_6 = 1024;
	private int FREE_CLASS_1 = 2048;
	private int FREE_CLASS_2 = 4096;
	private List<Button> ASSIST_EFFECT_BUTTONS = new ArrayList<Button>();
	private int[] ASSIST_EFFECT_INDEX = new int[2];
	private int[] ASSIST_EFFECT_VALUE = new int[2];		//保存辅助功效的两个数值
	private int MAIN_EFFECT = 0;	//主要功效的数值
	private int ASSIST_EFFECT = 0;	//辅助功效的数值
	private int VALUE_CLASS = 0;	//课程选择的总数值(传到课程网页的参数)

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// ##layout布局文件转化为view
		//resource 需要加载的布局文件
		//root 加载Layout的布局文件的父ViewGroup
		//布尔值变量 :false 不返回父ViewGroup
		View view = inflater.inflate(R.layout.layout_fragment_class, container, false);

		initView(view);

		return view;
	}

	private void initView(View view){
		layout_checkeffect = (RelativeLayout) view.findViewById(R.id.layout_checkeffect);

		bt_maineffect1 = (Button) view.findViewById(R.id.classfrag_maineffect1);
		bt_maineffect2 = (Button) view.findViewById(R.id.classfrag_maineffect2);
		bt_maineffect3 = (Button) view.findViewById(R.id.classfrag_maineffect3);
		bt_maineffect4 = (Button) view.findViewById(R.id.classfrag_maineffect4);
		bt_maineffect5 = (Button) view.findViewById(R.id.classfrag_maineffect5);
		bt_maineffect1.setOnClickListener(this);
		bt_maineffect2.setOnClickListener(this);
		bt_maineffect3.setOnClickListener(this);
		bt_maineffect4.setOnClickListener(this);
		bt_maineffect5.setOnClickListener(this);

		bt_assisteffect1 = (Button) view.findViewById(R.id.classfrag_assisteffect1);
		bt_assisteffect2 = (Button) view.findViewById(R.id.classfrag_assisteffect2);
		bt_assisteffect3 = (Button) view.findViewById(R.id.classfrag_assisteffect3);
		bt_assisteffect4 = (Button) view.findViewById(R.id.classfrag_assisteffect4);
		bt_assisteffect5 = (Button) view.findViewById(R.id.classfrag_assisteffect5);
		bt_assisteffect6 = (Button) view.findViewById(R.id.classfrag_assisteffect6);
		bt_assisteffect1.setOnClickListener(this);
		bt_assisteffect2.setOnClickListener(this);
		bt_assisteffect3.setOnClickListener(this);
		bt_assisteffect4.setOnClickListener(this);
		bt_assisteffect5.setOnClickListener(this);
		bt_assisteffect6.setOnClickListener(this);

		bt_freeclass1 = (Button) view.findViewById(R.id.classfrag_freeclass1);
		bt_freeclass2 = (Button) view.findViewById(R.id.classfrag_freeclass2);
		bt_freeclass1.setOnClickListener(this);
		bt_freeclass2.setOnClickListener(this);

		bt_openclass = (Button) view.findViewById(R.id.bt_openclass);
		bt_openclass.setOnClickListener(this);

		ASSIST_EFFECT_BUTTONS.add(bt_assisteffect1);
		ASSIST_EFFECT_BUTTONS.add(bt_assisteffect2);
		ASSIST_EFFECT_BUTTONS.add(bt_assisteffect3);
		ASSIST_EFFECT_BUTTONS.add(bt_assisteffect4);
		ASSIST_EFFECT_BUTTONS.add(bt_assisteffect5);
		ASSIST_EFFECT_BUTTONS.add(bt_assisteffect6);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			//主要功效
			case R.id.classfrag_maineffect1:
				MAIN_EFFECT = MAIN_EFFECT_1;
				bt_maineffect1.setBackgroundResource(R.color.softred);
				bt_maineffect2.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect3.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect4.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect5.setBackgroundResource(R.drawable.class_btbg);
				bt_freeclass1.setBackgroundResource(R.drawable.class_btbg);
				bt_freeclass2.setBackgroundResource(R.drawable.class_btbg);
				break;
			case R.id.classfrag_maineffect2:
				MAIN_EFFECT = MAIN_EFFECT_2;
				bt_maineffect1.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect2.setBackgroundResource(R.color.softred);
				bt_maineffect3.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect4.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect5.setBackgroundResource(R.drawable.class_btbg);
				bt_freeclass1.setBackgroundResource(R.drawable.class_btbg);
				bt_freeclass2.setBackgroundResource(R.drawable.class_btbg);
				break;
			case R.id.classfrag_maineffect3:
				MAIN_EFFECT = MAIN_EFFECT_3;
				bt_maineffect1.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect2.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect3.setBackgroundResource(R.color.softred);
				bt_maineffect4.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect5.setBackgroundResource(R.drawable.class_btbg);
				bt_freeclass1.setBackgroundResource(R.drawable.class_btbg);
				bt_freeclass2.setBackgroundResource(R.drawable.class_btbg);
				break;
			case R.id.classfrag_maineffect4:
				MAIN_EFFECT = MAIN_EFFECT_4;
				bt_maineffect1.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect2.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect3.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect4.setBackgroundResource(R.color.softred);
				bt_maineffect5.setBackgroundResource(R.drawable.class_btbg);
				bt_freeclass1.setBackgroundResource(R.drawable.class_btbg);
				bt_freeclass2.setBackgroundResource(R.drawable.class_btbg);
				break;
			case R.id.classfrag_maineffect5:
				MAIN_EFFECT = MAIN_EFFECT_5;
				bt_maineffect1.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect2.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect3.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect4.setBackgroundResource(R.drawable.class_btbg);
				bt_maineffect5.setBackgroundResource(R.color.softred);
				bt_freeclass1.setBackgroundResource(R.drawable.class_btbg);
				bt_freeclass2.setBackgroundResource(R.drawable.class_btbg);
				break;
			//辅助功效
			case R.id.classfrag_assisteffect1:
				setAssistEffect(1, ASSIST_EFFECT_1);
				break;
			case R.id.classfrag_assisteffect2:
				setAssistEffect(2, ASSIST_EFFECT_2);
				break;
			case R.id.classfrag_assisteffect3:
				setAssistEffect(3, ASSIST_EFFECT_3);
				break;
			case R.id.classfrag_assisteffect4:
				setAssistEffect(4, ASSIST_EFFECT_4);
				break;
			case R.id.classfrag_assisteffect5:
				setAssistEffect(5, ASSIST_EFFECT_5);
				break;
			case R.id.classfrag_assisteffect6:
				setAssistEffect(6, ASSIST_EFFECT_6);
				break;
			case R.id.classfrag_freeclass1:
				setFreeClass(bt_freeclass1,bt_freeclass2,FREE_CLASS_1,FREE_CLASS_2);
				break;
			case R.id.classfrag_freeclass2:
				setFreeClass(bt_freeclass2,bt_freeclass1,FREE_CLASS_2,FREE_CLASS_1);
				break;
			case R.id.bt_openclass:
				if(MAIN_EFFECT != 0){
					VALUE_CLASS = MAIN_EFFECT + ASSIST_EFFECT;
					Log.i("VALUE_CLASS", VALUE_CLASS+"="+MAIN_EFFECT+"+"+ASSIST_EFFECT);
				}
				if(VALUE_CLASS != 0){
					Intent intent = new Intent(getActivity(), ClassActivity.class);
					intent.putExtra("VALUE_CLASS", VALUE_CLASS);
					startActivity(intent);
				}else{
					showToast("您还未选择课程哦");
				}
				break;
			default:break;
		}
	}

	/*辅助功效按钮的处理，设置辅助功效，参数：(被点击的按钮的索引，被点击按钮的参数值)*/
	private void setAssistEffect(int aebtlist_index, int ae_value){
		if(MAIN_EFFECT != 0){
			if(aebtlist_index != ASSIST_EFFECT_INDEX[0] && aebtlist_index != ASSIST_EFFECT_INDEX[1]){
				if(ASSIST_EFFECT_INDEX[0] == 0){
					ASSIST_EFFECT_INDEX[0] = aebtlist_index;
					ASSIST_EFFECT_VALUE[0] = ae_value;
				}else if(ASSIST_EFFECT_INDEX[1] == 0){
					ASSIST_EFFECT_INDEX[1] = aebtlist_index;
					ASSIST_EFFECT_VALUE[1] = ae_value;
				}else{
					ASSIST_EFFECT_INDEX[0] = ASSIST_EFFECT_INDEX[1];
					ASSIST_EFFECT_INDEX[1] = aebtlist_index;
					ASSIST_EFFECT_VALUE[0] = ASSIST_EFFECT_VALUE[1];
					ASSIST_EFFECT_VALUE[1] = ae_value;
				}
				//按钮背景设置
				bt_assisteffect1.setBackgroundResource(R.drawable.class_btbg);
				bt_assisteffect2.setBackgroundResource(R.drawable.class_btbg);
				bt_assisteffect3.setBackgroundResource(R.drawable.class_btbg);
				bt_assisteffect4.setBackgroundResource(R.drawable.class_btbg);
				bt_assisteffect5.setBackgroundResource(R.drawable.class_btbg);
				bt_assisteffect6.setBackgroundResource(R.drawable.class_btbg);
				try {
					ASSIST_EFFECT_BUTTONS.get(ASSIST_EFFECT_INDEX[0]-1).setBackgroundResource(R.color.softred);
					ASSIST_EFFECT_BUTTONS.get(ASSIST_EFFECT_INDEX[1]-1).setBackgroundResource(R.color.softred);
					Log.i("fuck",ASSIST_EFFECT_INDEX[0]-1+"" );
				}catch (Exception e){
					Log.i("AEBT_Setbg_Exception",e.getMessage());
				}
			}else{
				ASSIST_EFFECT_BUTTONS.get(aebtlist_index-1).setBackgroundResource(R.drawable.class_btbg);
				if(ASSIST_EFFECT_INDEX[0] == aebtlist_index){
					ASSIST_EFFECT_INDEX[0] = ASSIST_EFFECT_INDEX[1];
					ASSIST_EFFECT_INDEX[1] = 0;
					ASSIST_EFFECT_VALUE[0] = ASSIST_EFFECT_VALUE[1];
					ASSIST_EFFECT_VALUE[1] = 0;
				}else{
					ASSIST_EFFECT_INDEX[1] = 0;
					ASSIST_EFFECT_VALUE[1] = 0;
				}
			}
			//设置ASSIST_EFFECT值
			ASSIST_EFFECT = ASSIST_EFFECT_VALUE[0] + ASSIST_EFFECT_VALUE[1];
			Log.i("value_ASSIST_EFFECT", ASSIST_EFFECT_VALUE[0]+"+"+ASSIST_EFFECT_VALUE[1]+"="+ASSIST_EFFECT);
			Log.i("index_ASSIST_EFFECT", ASSIST_EFFECT_INDEX[0]+"//"+ASSIST_EFFECT_INDEX[1]);
		}else{
			showToast("请先选择主要功效");
		}
	}

	/*免费课程按钮的处理，设置免费课程，参数:(被点击的免费课程按钮，未点击的按钮，被点击按钮的参数值，未点击按钮的参数)*/
	private void setFreeClass(Button bt_set, Button bt_unset, int value_set, int value_unset){
		if(VALUE_CLASS != value_set){
			bt_unset.setBackgroundResource(R.drawable.class_btbg);
			bt_set.setBackgroundResource(R.color.softred);
			VALUE_CLASS = value_set;
		}else{
			bt_set.setBackgroundResource(R.drawable.class_btbg);
			VALUE_CLASS = 0;
		}//清除功效选择
		MAIN_EFFECT = 0;
		ASSIST_EFFECT = 0;
		for(int i=0; i < ASSIST_EFFECT_BUTTONS.size(); i++){
			ASSIST_EFFECT_BUTTONS.get(i).setBackgroundResource(R.drawable.class_btbg);
		}
		bt_maineffect1.setBackgroundResource(R.drawable.class_btbg);
		bt_maineffect2.setBackgroundResource(R.drawable.class_btbg);
		bt_maineffect3.setBackgroundResource(R.drawable.class_btbg);
		bt_maineffect4.setBackgroundResource(R.drawable.class_btbg);
		bt_maineffect5.setBackgroundResource(R.drawable.class_btbg);
		ASSIST_EFFECT_INDEX[0] = 0;
		ASSIST_EFFECT_INDEX[1] = 0;
		ASSIST_EFFECT_VALUE[0] = 0;
		ASSIST_EFFECT_VALUE[1] = 0;
	}

	public void showToast(String str){
		// Toast的刷新时间为3s
		long secondTimeOfToast = System.currentTimeMillis();
		if (secondTimeOfToast - firstTimeOfToast > 3000) {
			Toast.makeText(getActivity(),str,Toast.LENGTH_SHORT).show();
			firstTimeOfToast = secondTimeOfToast;
		}
	}
}
